
import { elmessage } from "@/hook/hook"

export default {
  // hr登录管理企业信息的仓库
  state: {
    isEditEInfo: false,
    isEditEAdvantage: false,
    isEdiEContact: false,
    isEditBusinessInfo: false,
    EBasicInfo: {
      EId: "",
      EAvatar: "",
      EContent: ``,
      // 公司规模
      ESizeMin: "",
      ESizeMax: "",
      ENature: "",
      EIndustry: ""
    },
    EAdvantage: [],
    EContact: {
      contactName: "",
      contactPhone: "",
      contactEmail: "",
      contactAddress: ""
    },
    EBusinessInfo: {
      businessName: "",
      businessSetTime: "",
      // 注册资本
      businessRegCapital: "",
      // 法人代表
      businessLegalRepresentative: "",

    },

    EJobPublishings: [],
    EHRs: [

    ],
    ApplicantResumes: [

    ],
    previewUserResumes: [
    ],

    jobSeekerJobEx:[{
      X:[],
      Y:[],
    }],
    jobSeekerEdu:[{
      X:[],
      Y:[],
    }]
    
  },
  actions: {},
  mutations: {
    upEBasicInfo(state: any, info: any) {
      state.EBasicInfo.EContent = info.content
      state.EBusinessInfo.businessName = info.name

      state.EBasicInfo.ENature = info.nature
      state.EBasicInfo.EIndustry = info.industry
      state.EBasicInfo.ESizeMin = info.sizeMin
      state.EBasicInfo.ESizeMax = info.sizeMax

    },
    upEAvantage(state: any, advantage: any) {
      state.EAdvantage = advantage
    },
    upEContact(state: any, contacts: any) {
      state.EContact.contactName = contacts.contactName
      state.EContact.contactPhone = contacts.contactPhone
      state.EContact.contactEmail = contacts.contactEmail
      state.EContact.contactAddress = contacts.contactAddress
    },
    upEBusinessInfo(state: any, businessInfos: any) {
      state.EBusinessInfo.businessName = businessInfos.businessName
      state.EBusinessInfo.businessSetTime = businessInfos.businessSetTime
      state.EBusinessInfo.businessRegCapital = businessInfos.businessRegCapital
      state.EBusinessInfo.businessLegalRepresentative = businessInfos.businessLegalRepresentative
    },


    deleteJobPublishings(state: any, row: any) {
      const isfind1 = state.EJobPublishings.findIndex((item: any) => item.jobId === row.jobId)
      if (isfind1 != -1) {
        state.EJobPublishings.splice(isfind1, 1)
      }
      // console.log(state.EHRs);

      state.EHRs.forEach((item1: any, index: any, self: any) => {
        if (item1.hrName == row.hrName) {
          const isfind2 = item1.jobId.findIndex((item2: any) => item2 == row.jobId)
          if (isfind2 != -1) {
            item1.jobId.splice(isfind2, 1)
          }
        }

      })


    },
    upEJobPublishings(state: any, form: any) {
      for (let i = 0; i < state.EJobPublishings.length; i++) {
        if (form.jobId === state.EJobPublishings[i].jobId) {
          state.EJobPublishings[i].jobName = form.jobName
          state.EJobPublishings[i].jobAddress = form.jobAddress
          state.EJobPublishings[i].jobEdu = form.jobEdu
          state.EJobPublishings[i].jobEx = form.jobEx
          state.EJobPublishings[i].jobSalary = form.jobSalary
        }
      }
      state.EHRs.forEach((value: any, index: any, self: any) => {
        // 修改岗位的HR
        // 先判断表单选择的HR存不存在这个jobId

        if (value.hrName == form.hrName) {
          if (value.jobId.includes(form.jobId)) {
          
           
          } else {
            
            value.jobId.push(form.jobId)
          }
        }
        else {
          // 判断其他HR上有没有这个jobId
          const isfind = value.jobId.findIndex((item: any) => item == form.jobId)
          if (isfind != -1) {
            console.log("我在其他HR上发现有这个jobId，我需要删掉他");

            value.jobId.splice(isfind, 1)
          }
        }
      })

      // console.log(state.EHRs);

    },
    addEJobPublishings(state: any, form: any) {
      const jobId = new Date().getTime().toString();
      state.EJobPublishings.push({
        jobId: jobId,
        jobName: form.jobName,
        jobAddress: form.jobAddress,
        jobEdu: form.jobEdu,
        jobEx: form.jobEx,
        jobSalary: form.jobSalary,

      })
      // 给对应的HR添加jobId
      for (let j = 0; j < state.EHRs.length; j++) {
        if (state.EHRs[j].hrName == form.hrName) {
          state.EHRs[j].jobId.push(jobId)
        }
      }
      // console.log(state.EHRs);

    },

    deleteEHRs(state: any, row: any) {
      const isfind = state.EHRs.findIndex((item: any) => item.hrId === row.hrId)
      if (isfind != -1) {
        state.EHRs.splice(isfind, 1)
      }
      // // 查找该HR对应的下标
      // let isdel=false

      // state.EHRs.forEach((value: any, index: any, self: any) => {
      //   if (value.hrId == row.hrId) {
      //     console.log(value.jobId);
      //     // console.log(row.jobId.toString);

      //     console.log();

      //     if (value.jobId.length>0) {
      //       elmessage("该HR未交接完工作，无法删除", "warning")
      //       isdel= false
      //     } else {


      //     }
      //   }

      // })

      // return isdel
    },

    upEHRs(state: any, form: any) {
      // 修改HR信息
      state.EHRs.forEach((value: any, index: any, self: any) => {
        if (value.hrId == form.hrId) {
          value.hrName = form.hrName
          value.hrLevel = form.hrLevel
          value.hrPhone = form.hrPhone
          value.hrEmail = form.hrEmail
        }
      })
    },
    addEHRs(state: any, form: any) {
      const hrId = new Date().getTime().toString();
      state.EHRs.push({
        hrId: hrId,
        hrName: form.hrName,
        hrPhone: form.hrPhone,
        hrLevel: form.hrLevel,
        hrEmail: form.hrEmail,
        jobId: [],
      })
    },





    editEInfo(state: any) {
      state.isEditEInfo = !state.isEditEInfo
    },
    editEAdvantage(state: any) {
      state.isEditEAdvantage = !state.isEditEAdvantage
    },
    editEContact(state: any) {
      state.isEdiEContact = !state.isEdiEContact
    },
    editEBusinessInfo(state: any) {
      state.isEditBusinessInfo = !state.isEditBusinessInfo
    },
  },
  getters: {
    getIsDel(state: any) {
      return state.deleteEHRs
    }


  }

}